package leetcode.code0978;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int maxTurbulenceSize(int[] arr) {
		int len = arr.length;
		if (len == 1) {
			return 1;
		}
		int ans = arr[0] == arr[1] ? 1 : 2;
		if (len == 2) {
			return ans;
		}
		int flag = arr[0] == arr[1] ? 0 : arr[0] < arr[1] ? -1 : 1;
		for (int i = 2, cur = ans; i < len; i++) {
			int n0 = arr[i - 1], n1 = arr[i];
			if (n0 == n1) {
				cur = 1;
				flag = 0;
			} else if (n0 > n1) {
				if (flag <= 0) {
					cur += 1;
				} else {
					cur = 2;
				}
				flag = 1;
			} else {
				if (flag >= 0) {
					cur += 1;
				} else {
					cur = 2;
				}
				flag = -1;
			}
			ans = Math.max(ans, cur);
		}
		return ans;
	}

	@Override
	public void debug4() {
		// [9,4,2,10,7,8,8,1,9]
		H.compare(1, this.maxTurbulenceSize(H.str2array3("[16]")));
		H.compare(2, this.maxTurbulenceSize(H.str2array3("[4,8,12,16]")));
		H.compare(5, this.maxTurbulenceSize(H.str2array3("[9,4,2,10,7,8,8,1,9]")));
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
